package com.hp.gxw.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hp.gxw.constant.R;
import com.hp.gxw.entity.CostEquipment;
import com.hp.gxw.service.ICostEquipmentService;
import com.hp.gxw.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

@Component
@RequestMapping("/api/equipment")
public class EquipmentApi {

    @Autowired
    private ICostEquipmentService iCostEquipmentService;

    /**
     * 设备档案接口
     * @param map
     * @return
     */
    @RequestMapping("/register")
    @ResponseBody
    public R equipment_register(@RequestBody Map<String, Object> map){
        if(map == null){
            return R.error("参数为空");
        }
        if(map.get("code") == null){
            return R.error("code为必填项");
        }
        CostEquipment equipment = iCostEquipmentService.getOne(new LambdaQueryWrapper<CostEquipment>().eq(CostEquipment::getCode, map.get("code")));
        if(equipment == null){
            equipment = new CostEquipment();
            equipment.setCreateTime(new Date());
        }
        equipment.setCode(map.get("code").toString());
        if(map.get("name") == null){
            return R.error("name为必填项");
        }
        equipment.setName(map.get("name").toString());
        if(map.get("model") != null){
            equipment.setModel(map.get("model").toString());
        }
        if(map.get("entryTime") != null){
            equipment.setEntryTime(map.get("entryTime").toString());
        }
        if(map.get("manufacturer") != null){
            equipment.setManufacturer(map.get("manufacturer").toString());
        }
        if(map.get("source") == null){
            return R.error("source为必填项");
        }
        if(!(map.get("source").toString().equals("自购") || map.get("source").toString().equals("租赁"))){
            return R.error("source的值必须为自购或者租赁");
        }
        equipment.setSource(map.get("source").toString());
        if(map.get("power") == null){
            return R.error("power为必填项");
        }
        try {
            equipment.setPower(new BigDecimal(map.get("power").toString()));
        }catch (NumberFormatException ne){
            return R.error("power必须为数字");
        }
        if(map.get("depreciationAmount") != null){
            try {
                equipment.setDepreciationAmount(new BigDecimal(map.get("depreciationAmount").toString()));
            }catch (NumberFormatException ne){
                return R.error("depreciationAmount必须为数字");
            }
        }
        if(map.get("depreciationYear") != null){
            try {
                equipment.setDepreciationYear(new BigDecimal(map.get("depreciationYear").toString()));
            }catch (NumberFormatException ne){
                return R.error("depreciationYear必须为数字");
            }
        }
        if(map.get("depreciationEndTime") != null){
            try {
                equipment.setDepreciationEndTime(DateUtil.getDateFormat(map.get("depreciationEndTime").toString()));
            }catch (Exception ne){
                return R.error("depreciationEndTime必须为yyyy-MM-dd格式");
            }
        }
        if(map.get("leaseAmount") != null){
            try {
                equipment.setLeaseAmount(new BigDecimal(map.get("leaseAmount").toString()));
            }catch (NumberFormatException ne){
                return R.error("leaseAmount必须为数字");
            }
        }
        if(map.get("leaseYear") != null){
            try {
                equipment.setLeaseYear(new BigDecimal(map.get("leaseYear").toString()));
            }catch (NumberFormatException ne){
                return R.error("leaseYear必须为数字");
            }
        }
        if(map.get("leaseEndTime") != null){
            try {
                equipment.setLeaseEndTime(DateUtil.getDateFormat(map.get("leaseEndTime").toString()));
            }catch (Exception ne){
                return R.error("leaseEndTime必须为yyyy-MM-dd格式");
            }
        }
        if(map.get("type") != null){
            equipment.setEntryTime(map.get("type").toString());
        }else {
            return R.error("type设备类型必填！");
        }
        try {
            iCostEquipmentService.saveOrUpdate(equipment);
        }catch (Exception e){
            e.printStackTrace();
            return R.error("接口异常");
        }
        return R.ok("写入成功");
    }
}
